
import { ES3DTileset, ESRtsFeatureEditing, SceneObjectPickedInfo } from "earthsdk3";
import { ESUeViewer } from "../../../ESUeViewer";
import { UeESSceneObject } from "../../../UeObjects/base";
import { bind, createNextAnimateFrameEvent, track } from "xbsj-base";


export class UeESRtsFeatureEditing<T extends ESRtsFeatureEditing = ESRtsFeatureEditing> extends UeESSceneObject<T> {
    static readonly type = this.register<ESRtsFeatureEditing, ESUeViewer>('ESUeViewer', ESRtsFeatureEditing.type, this);

    private _es3DTileset = this.dv(new ES3DTileset(`${this.sceneObject.id}_ES3DTileset`));
    get es3DTileset() { return this._es3DTileset; }

    constructor(sceneObject: T, ueViewer: ESUeViewer) {
        super(sceneObject, ueViewer);

        const viewer = ueViewer.viewer;
        if (!viewer) {
            console.warn(`viewer is undefined!`);
            return;
        }
        sceneObject.mainClass = this._es3DTileset;
        //属性同步
        {
            const tileset = this._es3DTileset;
            ueViewer.add(tileset);
            this.dispose(() => ueViewer.delete(tileset))
            this.d(track([tileset, 'allowPicking'], [sceneObject, 'allowPicking']));
            this.d(track([tileset, 'collision'], [sceneObject, 'collision']));
            this.d(bind([tileset, 'flyToParam'], [sceneObject, 'flyToParam']));
            this.d(bind([tileset, 'flyToParam'], [sceneObject, 'flyToParam']));
            this.d(bind([tileset, 'editing'], [sceneObject, 'editing']));
            // this.d(bind([tileset, 'rotationEditing'], [sceneObject, 'rotationEditing']));

            //此处url为服务地址，由服务端返回
            this.d(track([tileset, 'url'], [sceneObject, 'url']));
            this.d(track([tileset, 'actorTag'], [sceneObject, 'actorTag']));
            this.d(track([tileset, 'materialMode'], [sceneObject, 'materialMode']));
            this.d(track([tileset, 'highlight'], [sceneObject, 'highlight']));
            this.d(track([tileset, 'maximumScreenSpaceError'], [sceneObject, 'maximumScreenSpaceError']));
            this.d(track([tileset, 'highlightID'], [sceneObject, 'highlightID']));
            this.d(track([tileset, 'highlightColor'], [sceneObject, 'highlightColor']));

            this.d(bind([tileset, 'offset'], [sceneObject, 'offset']));
            this.d(bind([tileset, 'rotation'], [sceneObject, 'rotation']));

            this.d(track([tileset, 'czmImageBasedLightingFactor'], [sceneObject, 'czmImageBasedLightingFactor']));
            this.d(track([tileset, 'czmLuminanceAtZenith'], [sceneObject, 'czmLuminanceAtZenith']));
            this.d(track([tileset, 'czmMaximumMemoryUsage'], [sceneObject, 'czmMaximumMemoryUsage']));
            this.d(track([tileset, 'czmClassificationType'], [sceneObject, 'czmClassificationType']));
            this.d(track([tileset, 'czmStyleJson'], [sceneObject, 'czmStyleJson']));
            this.d(track([tileset, 'czmBackFaceCulling'], [sceneObject, 'czmBackFaceCulling']));
            this.d(track([tileset, 'czmDebugShowBoundingVolume'], [sceneObject, 'czmDebugShowBoundingVolume']));
            this.d(track([tileset, 'czmDebugShowContentBoundingVolume'], [sceneObject, 'czmDebugShowContentBoundingVolume']));

            this.d(track([tileset, 'colorBlendMode'], [sceneObject, 'colorBlendMode']));
            this.d(track([tileset, 'cacheBytes'], [sceneObject, 'cacheBytes']));

            this.d(track([tileset, 'clippingPlaneEnabled'], [sceneObject, 'clippingPlaneEnabled']));
            this.d(track([tileset, 'unionClippingRegions'], [sceneObject, 'unionClippingRegions']));
            this.d(track([tileset, 'clippingPlaneEdgeColor'], [sceneObject, 'clippingPlaneEdgeColor']));
            this.d(track([tileset, 'clippingPlaneEdgeWidth'], [sceneObject, 'clippingPlaneEdgeWidth']));
            this.d(track([tileset, 'materialParams'], [sceneObject, 'materialParams']));

            this.d(bind([tileset, 'supportEdit'], [sceneObject, 'supportEdit']));
            this.d(bind([tileset, 'clippingPlanesId'], [sceneObject, 'clippingPlanesId']));
            this.d(bind([tileset, 'clippingPlaneIds'], [sceneObject, 'clippingPlaneIds']));
            this.d(bind([tileset, 'flattenedPlaneId'], [sceneObject, 'flattenedPlaneId']));
            this.d(bind([tileset, 'flattenedPlaneEnabled'], [sceneObject, 'flattenedPlaneEnabled']));
            this.d(bind([tileset, 'clippingPlaneId'], [sceneObject, 'clippingPlaneId']));
            this.d(bind([tileset, 'excavateId'], [sceneObject, 'excavateId']));
        }
        //函数同步
        {
            const es3DTileset = this._es3DTileset;
            this.d(sceneObject.refreshTilesetEvent.don(() => { es3DTileset.refreshTileset(); }));
            this.d(sceneObject.highlightFeatureEvent.don((...arg) => { es3DTileset.highlightFeature(...arg); }));
            this.d(sceneObject.highlightFeatureAndFlyToEvent.don((...arg) => { es3DTileset.highlightFeatureAndFlyTo(...arg); }));
            this.d(sceneObject.setFeatureColorEvent.don((...arg) => { es3DTileset.setFeatureColor(...arg); }));
            this.d(sceneObject.setFeatureVisableEvent.don((...arg) => { es3DTileset.setFeatureVisable(...arg); }));
            this.d(sceneObject.resetFeatureStyleEvent.don((...arg) => { es3DTileset.resetFeatureStyle(...arg); }));
            this.d(sceneObject.flyInEvent.don((...arg) => { es3DTileset.flyIn(arg[0]); }));
            this.d(sceneObject.flyToEvent.don((...arg) => { es3DTileset.flyTo(arg[0]); }));
            this.d(sceneObject.calcFlyToParamEvent.don((...arg) => { es3DTileset.calcFlyToParam(...arg); }));
            this.d(sceneObject.calcFlyInParamEvent.don((...arg) => { es3DTileset.calcFlyInParam(...arg); }));
        }
        //事件同步
        {
            const es3DTileset = this._es3DTileset;
            this.d(es3DTileset.tilesetReady.don((tileset) => { sceneObject.tilesetReady.emit(tileset); }));

            this.d(es3DTileset.pickedEvent.don((pickedInfo) => {
                // @ts-ignore
                sceneObject.pickedEvent.emit(new SceneObjectPickedInfo(sceneObject, pickedInfo.childPickedInfo));
            }));
        }
        //显隐控制
        {
            const updateShow = () => {
                this.es3DTileset.show = sceneObject.show;
            }
            updateShow();
            this.d(sceneObject.showChanged.don(() => { updateShow() }));
        }
    }
}
